package chixing.day18.synchronized_demo;

public class Account {
    private String name;
    private double banlance;
    public Account(){}

    public Account(String name, double banlance) {
        this.name = name;
        this.banlance = banlance;
    }

    //存款
    /*
    *  synchronized : 锁的是account,最多只有一个线程可以获得该对象
    * */
    public synchronized double depoist(double money){
        double temp =this.banlance ;
        temp += money;
        try {
            Thread.sleep(5); //模拟实际存款代码需要执行的时间
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.banlance  = temp;
        System.out.println("存款后 当前余额是：" + this.banlance);
        return this.banlance;
    }
    //取款
    public synchronized double withdraw(double money){
        double temp =this.banlance ;
        temp -= money;
        try {
            Thread.sleep(8);//模拟实际存款代码需要执行的时间
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.banlance= temp;
        System.out.println("取款 当前余额是：" + this.banlance);
        return this.banlance;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getBanlance() {
        return banlance;
    }

    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", banlance=" + banlance +
                '}';
    }
}
